package com.appspot.bibtable.view.html;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;

import com.appspot.bibtable.UrlCollection;
import com.appspot.bibtable.model.UserInfo;
import com.appspot.bibtable.util.HtmlUtility;
import com.appspot.bibtable.view.Template;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class HtmlTemplate extends Template
{
	protected static final String newline = System.getProperty("line.separator");

	private String title;
	private HttpServletRequest request;
	
	@Override
	public void endContent(Writer out) throws IOException
	{
		out.write("</div>");
		printFooter(out);
		out.write("</body>");
		out.write(newline);
	}
	
	@Override
	public void endDocument(Writer out) throws IOException
	{
		out.write("</html>");
		out.write(newline);
	}
	
	@Override
	public void startContent(Writer out) throws IOException
	{
		out.write("<body>");
		out.write(newline);
		printHeader(out);
		out.write("<div id=\"container\">");
	}
	
	@Override
	public void startDocument(Writer out) throws IOException
	{
		out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		out.write(newline);
		out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
		out.write(newline);
		out.write("<head>");
		String title = getTitle();
		if (title == null)
		{
			out.write("<title>BibTable</title>");
		}
		else
		{
			out.write("<title>" + title + " - BibTable</title>");
		}
		printMeta(out);
		printStylesheet(out);
		printScript(out);
		out.write("</head>");
		out.write(newline);
	}

	public void printSearchBox(Writer out) throws IOException
	{
		out.write("<form action=\"/search\" method=\"get\">");
		out.write("<div id=\"searchbox\" class=\"unprintable\">");
		String query = getRequest().getParameter("query");
		if (query == null)
		{
			query = "";
		}
		out.write("<input type=\"text\" name=\"query\" value=\"" + HtmlUtility.encode(query) + "\" id=\"query\" maxlength=\"255\" />");
		out.write("<input type=\"submit\" value=\"Search\" />");
		out.write("</div>");
		out.write("</form>");
		out.write(newline);
	}
	
	protected void printMeta(Writer out) throws IOException
	{
		out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
		out.write("<meta name=\"copyright\" content=\"Copyright &#169; 2010 BibTable. All Rights Reserved.\" />");
	}
	
	protected void printStylesheet(Writer out) throws IOException
	{
		out.write("<link rel=\"stylesheet\" href=\"/themes/default/stylesheets/style.css\" media=\"all\" />");
		out.write("<link rel=\"stylesheet\" href=\"/themes/default/stylesheets/screen.css\" media=\"screen\" />");
		out.write("<link rel=\"stylesheet\" href=\"/themes/default/stylesheets/print.css\" media=\"print\" />");
		out.write("<link rel=\"stylesheet\" href=\"/themes/default/stylesheets/handheld.css\" media=\"handheld\" />");
	}
	
	protected void printScript(Writer out) throws IOException
	{
		out.write("<script src=\"/js/jquery.js\" type=\"text/javascript\"></script>");
		out.write("<script src=\"/js/bibtable.js\" type=\"text/javascript\"></script>");
	}

	protected void printHeader(Writer out) throws IOException
	{
		out.write("<div id=\"header\">");
		printStatus(out);
		out.write("<h1 id=\"title\"><a href=\"" + UrlCollection.INDEX + "\">BibTable</a></h1>");
		out.write("</div>");
		String title = getTitle();
		if (title != null)
		{
			out.write("<h2>" + title + "</h2>");
		}
	}

	protected void printFooter(Writer out) throws IOException
	{
		out.write("<div id=\"footer\">");
		printCopyright(out);
		printNavigation(out);
		printPoweredBy(out);
		out.write("</div>");
	}

	protected void printCopyright(Writer out) throws IOException
	{
		out.write("<div id=\"copyright\">");
		out.write("Copyright &#169; 2010 BibTable. All Rights Reserved.");
		out.write("</div>");
	}
	
	protected void printPoweredBy(Writer out) throws IOException
	{
		out.write("<div id=\"powered-by\" class=\"unprintable\">");
		out.write("<a href=\"http://code.google.com/intl/en/appengine/\">");
		out.write("<img src=\"http://code.google.com/appengine/images/appengine-noborder-120x30.gif\" alt=\"Powered by Google App Engine\" />");
		out.write("</a>");
		out.write("</div>");
	}

	protected void printNavigation(Writer out) throws IOException
	{
		out.write("<div id=\"navigation\" class=\"unprintable\">");
		out.write("<a href=\"" + UrlCollection.INDEX + "\">Home</a>");
		out.write(" <span class=\"splitter\">|</span> ");
		out.write("<a href=\"" + UrlCollection.PROJECT + "\">Project</a>");
		out.write(" <span class=\"splitter\">|</span> ");
		out.write("<a href=\"" + UrlCollection.TERMS_OF_USE + "\">Terms of Use</a>");
		out.write(" <span class=\"splitter\">|</span> ");
		out.write("<a href=\"" + UrlCollection.TIPS + "\">Tips</a>");
		out.write(" <span class=\"splitter\">|</span> ");
		out.write("<a href=\"" + UrlCollection.HELP + "\">Help</a>");
		out.write("</div>");
	}

	protected void printStatus(Writer out) throws IOException
	{
		UserService service = UserServiceFactory.getUserService();
		HttpServletRequest request = getRequest();
		String requestUri = request.getRequestURI();
		String query = request.getQueryString();
		if (query != null)
		{
			requestUri += "?" + query;
		}

		out.write("<div id=\"status\" class=\"unprintable\">");
		if (service.isUserLoggedIn())
		{
			User user = service.getCurrentUser();
			UserInfo userInfo = UserInfo.getUserInfoOf(user);
			out.write("<span class=\"username\">" + user.getNickname() + "</span>");
			if (userInfo == null)
			{
				out.write(" (<a href=\"" + UrlCollection.REGISTRATION + "\">not registered</a>)");
			}
			else
			{
				out.write(" <span class=\"splitter\">|</span> ");
				out.write("<a href=\"" + UrlCollection.PROFILE + "\">Profile</a>");
			}
			out.write(" <span class=\"splitter\">|</span> ");
			out.write("<a href=\"" + service.createLogoutURL(requestUri) + "\">Sign out</a>");
		}
		else
		{
			out.write("Anonymous");
			out.write(" <span class=\"splitter\">|</span> ");
			out.write("<a href=\"" + service.createLoginURL(requestUri) + "\">Sign in</a>");
		}
		out.write("</div>");
		out.write(newline);
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public HttpServletRequest getRequest()
	{
		return request;
	}

	public void setRequest(HttpServletRequest request)
	{
		this.request = request;
	}
}
